<template>
	<view>
		<!-- 搜索栏 -->
		<cus-search @seachHandler="toSearch"></cus-search>
		<view class="scroll-view-container">
			<!-- 左侧滚动区域 -->
			<scroll-view scroll-y="true" class="left-scroll-view" :style="{height: wh + 'px'}">
				<view :class="['left-scroll-view-item', index === active ? 'active' : '' ]" v-for="(item, index) in cateList" :key="index" @click="cahngeIndex(index)">{{item.cat_name}}</view>
				
			</scroll-view>
			<!-- 右侧滚动区域 -->
			<scroll-view scroll-y="true" class="right-scroll-view" :style="{height: wh + 'px'}" :scroll-top="scrollTop">
				<view class="cate-lv2" v-for="(item, index) in cateLevel" :key="index">
					<view class="cate-lv2-title">
						{{item.cat_name}}
						<view class="cate-lv3">
							<view class="cate-lv3-item" v-for="(subitem, i) in item.children" :key="i" @click="toGoodsList(i)">
								<image :src="subitem.cat_icon" mode=""></image>
								<text>{{subitem.cat_name}}</text>
							</view>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>	
	</view>
</template>

<script>
	import mixBadge from '@/mixins/tabbar-badge.js'
	export default {
		mixins: [mixBadge],
		data() {
			return {
				active: 0,
				// 定义可用高度
				wh: 0,
				// 分类数据列表
				cateList:[],
				// 二级分类列表
				cateLevel:[],
				// 回到顶部
				scrollTop: 0
			}
		},
		methods: {
			// 获取分类数据列表
			async getCateList() {
				const {data: res} = await uni.$http.get('/api/public/v1/categories')
				// console.log(res);
				if(res.meta.status !== 200) return uni.$showMsg()
				this.cateList = res.message
				// 二级分类列表
				this.cateLevel = res.message[0].children
			},
			
			//选中事件处理函数
			cahngeIndex(index) {
				this.active = index
				this.cateLevel = this.cateList[index].children
				// 回到顶部
				this.scrollTop = this.scrollTop === 0 ? 1 : 0 
			},
			
			// 跳转商品列表
			toGoodsList(i) {
				 uni.navigateTo({
				    url: '/subpkg/goods_list/goods_list?cid=' + i
				  })
			},
			
			// 跳转至搜索页面
			toSearch() {
				uni.navigateTo({
					url:'/subpkg/search/search'
				})
			}
		},
		onLoad() {
			var res = uni.getSystemInfoSync()
			// console.log(res);
			this.wh = res.windowHeight - 50
			
			this.getCateList()
		}
	}
</script>

<style lang="scss">
.scroll-view-container {
	display: flex;
	.left-scroll-view {
		width: 120px;
		.left-scroll-view-item {
			line-height: 60px;
			text-align: center;
			background-color: #f7f7f7;
			font-size: 12px;
			&.active {
				background-color: #fff;
				position: relative;
				&::before {
					content: "";
					display: block;
					width: 3px;
					height: 30px;
					background-color: #c00000;
					position: absolute;
					left: 0;
					top: 50%;
					transform: translateY(-50%);
				}
			}
		}
	}
}
.cate-lv2-title {
	font-weight: bold;
	font-size: 12px;
	padding: 15px 0;
	text-align: center;
}
.cate-lv3 {
	display: flex;
	flex-wrap: wrap;
	margin-top: 10px;
	.cate-lv3-item {
		width: 33.33%;
		margin-bottom: 10px;
		display: flex;
		flex-direction: column;
		align-items: center;
		image {
			width: 60px;
			height: 60px;
		}
		text {
			font-size: 12px;
			font-weight: 500;
		}
	}
}
</style>
